home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_02 / labrocca / extr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-01  |  2.2 KB  |  90 lines

  1. /* Listing 3 */
  2. /* EXTR.C */
  3. /*  Copyright 1993 by P.J. LaBrocca
  4.     All rights reserved.
  5. */
  6. /* The Extraction Module */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. #include "sea.h"
  13.  
  14. void bailout( void )
  15. {
  16.     printf( "\nUsage: archiveName [ -l ]\n" );
  17.     printf( "    archiveName     Extract files.\n" );
  18.     printf( "    archiveName -l  List files.\n" );
  19.     exit( 0 );
  20. }
  21.  
  22. void main( int argc, char **argv )
  23. {
  24.     FILE *input, *output;
  25. /* size of extraction module */
  26.     long MagicNumber = 10867 ; /* sic */
  27.     int count = 1;
  28.     HEADER header;
  29.     long i;
  30.     int sw;
  31.  
  32. /* verify user input */    
  33.     if( argc > 2 ) {
  34.         bailout();
  35.     }
  36.     if( argc == 1 )
  37.         ;
  38.     else if( strcmp( argv[1], "-l" ) == 0 )
  39.         sw = 'l';
  40.     else
  41.         bailout();
  42.  
  43. /* open self-extracting archive */
  44.     if( ( input = fopen( argv[0], "rb" ) ) == NULL ) {
  45.         printf( "error opening %s\n", argv[0] );
  46.         exit( 0 );
  47.     } /* if( ( input = */
  48.  
  49. /* skip extraction module */
  50.     fseek( input, MagicNumber, SEEK_SET );
  51.  
  52.     switch( sw ) {
  53.     default:          /* extract contents of archive */
  54.       while( 1 ) {
  55.         fread(&header, sizeof(HEADER), 1, input);
  56.         if(header.filesize == -1L)
  57.             break;
  58.         if((output=fopen(header.filename,"wb"))==NULL){
  59.           printf("error opening %s\n",header.filename);
  60.           exit(0);
  61.         } /* if(( output = */
  62.  
  63.         printf("Creating %-55s", header.filename);
  64.         for( i = 0; i < header.filesize; ++i ) {
  65.             putc( getc( input ), output );
  66.         } /* for( i = 0; i < */
  67.         printf("Done!\n");
  68.         fclose( output );
  69.         ++count;
  70.       } /* while(  ) */
  71.       break;
  72.     case 'l':         /* list contents of archive */
  73.       while( 1 ) {
  74.           fread(&header, sizeof(HEADER), 1, input);
  75.           if( header.filesize == -1L )
  76.               break;
  77.           printf(" %-15s%9ld\n", header.filename,
  78.                                      header.filesize);
  79. /* Skip file contents. */
  80.           fseek( input, header.filesize, SEEK_CUR );
  81.           ++count;
  82.       } /* while(  ) */
  83.       break;
  84.     } /* switch */
  85.         
  86.     fclose( input );
  87. } /* main */
  88.  
  89. /* End of File */
  90.